using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using DirectShowLib;

namespace IGlobalist
{
    public partial class FrmMediaConfig : Form
    {
        private const string DefaultAudioCompressorName = "DSP Group TrueSpeech(TM)";
        private const string DefaultVideoCompressorName = "Microsoft Windows Media Video 9";

        IBaseFilter vInput = null;
        IBaseFilter aInput = null;
        IBaseFilter vCompressor = null;
        IBaseFilter aCompressor = null;


        public FrmMediaConfig()
        {
            InitializeComponent();
        }

        private void FrmMediaConfig_Load(object sender, EventArgs e)
        {
            DsDevice[] aiDevices = DsDevice.GetDevicesOfCat(FilterCategory.AudioInputDevice);
            DsDevice[] viDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            Global.HasAudioInput = aiDevices.Length > 0;
            Global.HasVideoInput = viDevices.Length > 0;

            if (aiDevices.Length != 0)
            {
                foreach (DsDevice ds in aiDevices)
                {
                    cboAudioDevices.Items.Add(ds.Name);
                }
                cboAudioDevices.SelectedIndex = 0;
            }
            else
            {
                gbAudio.Enabled = false;
            }
            if (viDevices.Length != 0)
            {
                foreach (DsDevice ds in viDevices)
                {
                    cboVideoDevices.Items.Add(ds.Name);
                }
                cboVideoDevices.SelectedIndex = 0;
            }
            else
            {
                gbVideo.Enabled = false;
            }
            foreach (DsDevice ds in DsDevice.GetDevicesOfCat(FilterCategory.VideoCompressorCategory))
            {
                cboVideoCompressors.Items.Add(ds.Name);
                if (ds.Name.Equals(DefaultVideoCompressorName))
                {
                    Guid iid = typeof(IBaseFilter).GUID;
                    object obj = null;
                    ds.Mon.BindToObject(null, null, ref iid, out obj);
                    vCompressor = obj as IBaseFilter;
                    cboVideoCompressors.SelectedItem = ds.Name;
                }
            }
            foreach (DsDevice ds in DsDevice.GetDevicesOfCat(FilterCategory.AudioCompressorCategory))
            {
                cboAudioComressors.Items.Add(ds.Name);
                if (ds.Name.Equals(DefaultAudioCompressorName))
                {
                    Guid iid = typeof(IBaseFilter).GUID;
                    object obj = null;
                    ds.Mon.BindToObject(null, null, ref iid, out obj);
                    aCompressor = obj as IBaseFilter;
                    cboAudioComressors.SelectedItem = ds.Name;
                }
            }

        }

        #region "-----------------------------------------"

        #endregion

     
        private void btnVIConfig_Click(object sender, EventArgs e)
        {
            try
            {
                string devicepath = cboVideoDevices.SelectedItem.ToString();
                vInput = Global.CreateFilter(FilterCategory.VideoInputDevice, devicepath);
                Global.DisplayPropertyPage(vInput);
            }
            catch { }
            finally
            {
                if (vInput != null)
                {
                    Marshal.ReleaseComObject(vInput);
                    vInput = null;
                }
            }

        }

        private void btnVCConfig_Click(object sender, EventArgs e)
        {
            try
            {
                string devicepath = cboVideoCompressors.SelectedItem.ToString();
                vCompressor = Global.CreateFilter(FilterCategory.VideoCompressorCategory, devicepath);
                Global.DisplayPropertyPage(vCompressor);                
            }
            catch { }
            finally
            {
                if (vCompressor != null)
                {
                    Marshal.ReleaseComObject(vCompressor);
                    vCompressor = null;
                }
            }
        }

        private void btnAIConfig_Click(object sender, EventArgs e)
        {
            try
            {
                string devicepath = cboAudioDevices.SelectedItem.ToString();
                aInput = Global.CreateFilter(FilterCategory.AudioInputDevice, devicepath);
                Global.DisplayPropertyPage(aInput);
            }
            catch { }
            finally
            {
                if (aInput != null)
                {
                    Marshal.ReleaseComObject(aInput);
                    aInput = null;
                }
            }

        }

        private void btnACConfig_Click(object sender, EventArgs e)
        {
            try
            {
                string devicepath = cboAudioComressors.SelectedItem.ToString();
                aCompressor = Global.CreateFilter(FilterCategory.AudioCompressorCategory, devicepath);
                Global.DisplayPropertyPage(aCompressor);
            }
            catch {}
            finally
            {
                if (aCompressor != null)
                {
                    Marshal.ReleaseComObject(aCompressor);
                    aCompressor = null;
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (Global.HasAudioInput)
            {
                Global.AudioInputDeviceName = cboAudioDevices.SelectedItem.ToString();
                Global.AudioCompressorName = cboAudioComressors.SelectedItem.ToString();
            }
            if (Global.HasVideoInput)
            {
                Global.VideoInputDeviceName = cboVideoDevices.SelectedItem.ToString();
                Global.VideoCompressorName = cboVideoCompressors.SelectedItem.ToString();
            }
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (Global.HasAudioInput)
            {
                Global.AudioInputDeviceName = cboAudioDevices.Items[0].ToString();
            }
            if (Global.HasVideoInput)
            {
                Global.VideoInputDeviceName = cboVideoDevices.Items[0].ToString();
            }
            this.Close();
        }      
    }
}